package com.web.manager.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.pojo.Result;
import com.web.manager.service.NoticeService;
import com.web.manager.service.UserService;
import com.web.manager.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;
    private UserService userService;


    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody Notice notice){


        notice.setTime(DateUtil.now());

        noticeService.save(notice);
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice){
        noticeService.updateById(notice);  //updateUser
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        noticeService.removeById(id);  //deleteUser
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        noticeService.removeBatchByIds(ids);   //batchDeleteUser
        return Result.success();
    }

    /*
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<User> userList = userService.list();  //selectAll
        return Result.success(userList);
    }

     */
    //查询公告
    @GetMapping("/selectUserData")
    public Result selectUserData(){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>().orderByDesc("id");
        queryWrapper.eq("open",1);
        List<Notice> noticeList = noticeService.list(queryWrapper);  //selectAll
        return Result.success(noticeList);
    }




    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){

        Notice notice= noticeService.getById(id);   //selectById
        return Result.success(notice);
    }





    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String title) {

        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();  //条件构造器
        queryWrapper.like(StrUtil.isNotBlank(title),"title",title);

        //select * from manager_user where username like '%#{username}% and name like '%#{name}%'

        Page<Notice> page = noticeService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
        return Result.success(page);
    }
}


